'use strict';

const Controller = require('egg').Controller;

class ApplyController extends Controller {
  async addFriend() {
    //当前用户id
    let currentuserid = this.ctx.authUser.id;
    this.ctx.validate({
      friend_id: {
        type: "int",
        required: true,
        desc: "用户_id",
      },
      nickname: { type: "string", required: true, desc: "昵称" },
      lookme: {
        type: "int", required: true,
        range: {
          in: [0, 1]
        },
        desc: "看我"
      },
      lookhim: {
        type: "int", required: true, range: {
          in: [0, 1]
        }, desc: "看他"
      },


    });

    if (currentuserid === this.ctx.request.body.friend_id) {
      this.ctx.throw(400, "不能添加自己")
    }
    let user = await this.app.model.User.findOne({
      where: {
        id: this.ctx.request.body.friend_id,
        status: 1

      }
    })
    if (!user) {
      this.ctx.throw(400, "该用户不存在或已被禁用")

    }
    let apply = await this.app.model.Apply.findOne({
      where: {
        user_id: currentuserid,
        friend_id: this.ctx.request.body.friend_id,
        status: ["pending", "agree"]
      }
    })
    if (apply) {
      this.ctx.throw("你之前已申请过他了")
    }
    let res = await this.app.model.Apply.create({

      user_id: currentuserid,
      friend_id: this.ctx.request.body.friend_id,
      nickname: this.ctx.request.body.nickname,
      lookme: this.ctx.request.body.lookme,
      lookhim: this.ctx.request.body.lookhim



    });
    if (!res) {
      this.ctx.throw(400, "申请失败")
    }
    this.ctx.apiSuccess(res, "成功", 200)

  }
  async list() {
    let currentuserid = this.ctx.authUser.id;
    let page = this.ctx.params.page ? parseInt(this.ctx.params.page) : 1
    let limit = this.ctx.params.limit ? parseInt(this.ctx.params.limit) : 10000
    let offset = (page - 1) * limit;
    const res = await this.ctx.model.Apply.findAll({
      where: {
        friend_id: currentuserid,

      },
      include: [{
        model: this.app.model.User,
        attributes: ["id", "username", "nickname", "avatar"],
      }],
      offset,
      limit
    })
    let count = await this.ctx.model.Apply.count({
      where: {
        friend_id: currentuserid,
        status: "pending"
      }
    })
    res.count = count
    console.log(res.count, count, "count")
    this.ctx.apiSuccess({
      res, count
    }, "获取成功", 200)
  }
  async handle() {
    let id = this.ctx.params.id;
    let currentuserid = this.ctx.authUser.id;
    this.ctx.validate({
      friend_id: {
        type: "int",
        required: true,
        desc: "用户_id",
      },
      nickname: { type: "string", required: true, desc: "昵称" },
      lookme: {
        type: "int", required: true,
        range: {
          in: [0, 1]
        },
        desc: "看我"
      },
      lookhim: {
        type: "int", required: true, range: {
          in: [0, 1]
        }, desc: "看他"
      },
      status: {
        type: "string",
        required: true,
        range: {
          in: ["agree", "ignore", "refuse"]
        },
        desc: "处理结果",
      }


    });
    let apply =await this.app.model.Apply.findOne({
      where: {

        friend_id: currentuserid,
        status: "pending"
      }
    })
    if (!apply) {
      this.ctx.throw(400, "该记录不存在")
    }
    let transaction;
    try {
      // 开启事务
      transaction = await this.app.model.transaction();
      await apply.update({
        status: this.ctx.request.body.status
      }, { transaction })
      if (this.ctx.request.body.status == 'agree') {
      console.log(apply.user_id,currentuserid,"currentuserid")
      console.log(await this.app.model.Friend.findOne({
        where:{
          friend_id: currentuserid,
          user_id: apply.user_id
       
        }
        }),"1233334")
        //自己加入到对方列表
        if (!await this.app.model.Friend.findOne({
          where:{
          friend_id: currentuserid,
          user_id: apply.user_id
          }
        })) {
          await this.app.model.Friend.create({
            friend_id: currentuserid,
            user_id: apply.user_id,
            nickname: apply.nickname,
            lookme: apply.lookme,
            lookhim: apply.lookhim
          })
          console.log(456)
          await this.app.model.Friend.create({
            friend_id: apply.user_id,
            user_id: currentuserid,
            nickname: this.ctx.request.body.nickname,
            lookme: this.ctx.request.body.lookme,
            lookhim: this.ctx.request.body.lookhim
          })
          console.log(789)
        }
        //对方加入到自己的好友列表
        console.log(!await this.app.model.Friend.findOne({
          friend_id: apply.user_id,
          user_id: currentuserid
        }),"eer")
        if (!await this.app.model.Friend.findOne({
          where:{
          friend_id: apply.user_id,
          user_id: currentuserid
          }
        })) {
          console.log(123)
          await this.app.model.Friend.create({
            friend_id: apply.user_id,
            user_id: currentuserid,
            nickname: this.ctx.request.body.nickname,
            lookme: this.ctx.request.body.lookme,
            lookhim: this.ctx.request.body.lookhim
          })
        }
      }
      // 提交事务
      await transaction.commit();
      // 消息推送
      return this.ctx.apiSuccess("操作成功");
    } catch (e) {
      console.log(e,"e")
      // 事务回滚
      // await transaction.rollback();
      return this.ctx.apiFail("操作失败");
    }
    this.ctx.apiSuccess("处理成功")
  }
}

module.exports = ApplyController;
